package com.zhangzeqin.code.view;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zhangzeqin.code.controller.AdminController;
import com.zhangzeqin.code.controller.UserController;
import com.zhangzeqin.code.entity.User;
import com.zhangzeqin.code.util.JdbcUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Queue;

/**
 * 处理login界面按钮事件
 * 继承按钮事件(回车) KeyAdapter
 * 实现 ActionListener
 * @author 张泽钦
 */
public class LoginHandler extends KeyAdapter implements ActionListener {
    /**
     * 记住登录的账号
     */
    public static String user=null;
    private String password=null;
    private LoginView loginView;
    public RegisterView registerView;
    UserController userController=new UserController();
    AdminController adminController=new AdminController();
    public LoginHandler(LoginView loginView){
        this.loginView=loginView;
    }
    public LoginHandler(RegisterView registerView){
        this.registerView=registerView;
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton)e.getSource();
        String text = jButton.getText();
        if("登录".equals((text))){
            try {
                login();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else if("注册".equals(text)){
            new RegisterView();


        }
    }

    private void login() throws SQLException {
         user = loginView.getUserTxt().getText();
         password = loginView.getPasswordField().getText();

        //查询
        boolean check = check(user, password);
        boolean flag=false ;
        if(loginView.num==0){
           flag = userController.landing(user, password);
            if (check) {
                if (flag) {
                    JOptionPane.showMessageDialog(loginView, "登录成功！");
                    new WordBookView();
                    newJFrame();
                } else {
                    JOptionPane.showMessageDialog(loginView, "用户名或密码错误");
                }
            }
        }else {
            flag=adminController.landing(user,password,loginView.table);
            if (check) {
                if (flag) {
                    JOptionPane.showMessageDialog(loginView, "登录成功！");
                    new AdminView();
                } else {
                    JOptionPane.showMessageDialog(loginView, "用户名或密码错误");
                }
            }
        }

    }
    /**
     * 回车触发"登录"按钮
     */
    @Override
    public void keyPressed(KeyEvent e) {
       if(KeyEvent.VK_ENTER== e.getKeyCode())
       {
           try {
               login();
           } catch (SQLException throwables) {
               throwables.printStackTrace();
           }
       }

    }
    private boolean check(String userName, String password) {

        if(userName ==null|| userName.length()<=0){
            JOptionPane.showMessageDialog(loginView, "用户名不能为空！");
            return false;
        }else
        {
            if(password ==null|| password.length()<=0){
                JOptionPane.showMessageDialog(loginView, "密码不能为空！");
                return false;
            }
        }
        return true;
    }
    /**
     * 创建一个子窗口
     */
    public static void newJFrame(){
        /**
         * "公告"文本区域,5行20列,"发布按钮"
         */
        JTextArea noticeField=new JTextArea(5,20);
        JFrame rankJFrame =new JFrame("公告");
        AdminController adminController=new AdminController();
        //设置窗口大小,不可修改大小
        rankJFrame.setSize(400,400);
        rankJFrame.setResizable(false);
        //关闭窗口时,不结束程序
        rankJFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //计算位置居中
        rankJFrame.setLocationRelativeTo(null);
        //添加组件
        JPanel jPanel=new JPanel(new GridLayout(1, 1));

        //"公告"文本框设置自动换行
        noticeField.setLineWrap(true);
        //初始化文本区域
        try {
            noticeField.setText((String) adminController.getNotice());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        rankJFrame.setContentPane(jPanel);
        rankJFrame.setVisible(true);
    }

}
